package com.tourex;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.google.gson.Gson;
import com.tourex.thirdparty.volley.BitmapCache;
import com.tourex.thirdparty.volley.VolleyUtils;
import com.tourex.ui.MainActivity2;
import com.tourex.ui.view.TitleLayout;
import com.tourex.utils.AnimUtils;
import com.tourex.utils.DialogUtils;

public class BaseActivity extends FragmentActivity implements Response.ErrorListener {

	public LayoutInflater inflater;
	public FragmentManager fragmentManager;
	public RequestQueue requestQueue;
	public ImageLoader imageLoader;
	public BitmapCache bitmapCache;
	public Gson gson;
	public TitleLayout titleLayout;
	public boolean useDefaultPendingTransition = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityCollector.addActivity(this);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		fragmentManager = getSupportFragmentManager();
		requestQueue = App.i().requestQueue;
		imageLoader = App.i().imageLoader;
		bitmapCache = App.i().bitmapCache;

		gson = new Gson();

		titleLayout = (TitleLayout) findViewById(R.id.titlelayout);

		VolleyUtils.init(requestQueue, this, this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
		requestQueue.cancelAll(App.i());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityCollector.removeActivity(this);
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		if (useDefaultPendingTransition) {
			overridePendingTransition(R.anim.activity_right_enter, R.anim.activity_left_exit);
		}
	}

	@Override
	public void finish() {
		super.finish();
		if (useDefaultPendingTransition) {
			if (ActivityCollector.size() > 1) {
				overridePendingTransition(R.anim.activity_left_enter, R.anim.activity_right_exit);
			}
		}
	}

	public void returnToTop() {
		final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview);
		if (scrollView != null)
			(scrollView).post(new Runnable() {
				@Override
				public void run() {
					scrollView.scrollTo(0, 0);
				}
			});
	}

	public DialogUtils createDialog(View content, boolean canCancel, boolean shadow) {
		return DialogUtils.init(this).create(content, canCancel, shadow);
	}

	public DialogUtils createSimpleDialog(int textId, boolean canCancel, boolean shadow) {
		return DialogUtils.init(this).createSimple(textId, canCancel, shadow);
	}

	public void saveToSharedPreferences(String key, Object value) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putString(key, value.toString()).commit();
	}

	public String getFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getString(key, null);
	}

	public void showToast(int textId) {
		showToast(getString(textId));
	}

	public void showToast(String text) {
		View view = inflater.inflate(R.layout.toast_message, null);
		((TextView) view.findViewById(R.id.tx_message)).setText(text);
		Toast mToast = new Toast(getApplicationContext());
		mToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.setView(view);
		mToast.show();
	}

	public void startBrowser(String url) {
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		intent.setData(Uri.parse(url));
		startActivity(intent);
	}

	public void startAnimation(View view, Animation animation) {
		view.setVisibility(View.VISIBLE);
		view.clearAnimation();
		view.startAnimation(animation);
	}

	public void endAnimation(View view, Animation animation) {
		view.clearAnimation();
		view.startAnimation(animation);
		view.setVisibility(View.GONE);
	}

	@Override
	public void onErrorResponse(VolleyError error) {
		// showToast(NetworkUtils.isDisconnected(this) ? R.string.error_net :
		// R.string.error_request);
	}

	public static ImageListener getDefaultImageListener(ImageView imageView) {
		return ImageLoader.getImageListener(imageView, R.color.image_load, R.color.image_error);
	}

	public OnClickListener onBackClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			startAnimation(view, AnimUtils.init().addAlpha(1f, 0.2f, 0, 200, new LinearInterpolator(), Animation.REVERSE).create());
			finish();
		}
	};

	public OnClickListener onToMainClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			startAnimation(view, AnimUtils.init().addAlpha(1f, 0.2f, 0, 200, new LinearInterpolator(), Animation.REVERSE).create());
			MainActivity2.actionStart(BaseActivity.this);
		}
	};

}
